/***************************************************************************
 *   Copyleft 2006-2007 by												   *
 *	 Iwanicki Krzysztof													   *
 *   <gamma.mini.pw.edu.pl/~iwanickik>									   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 ***************************************************************************/
#include "./ProjectionPlane.h"
#include "../../Macros/Macros.h"


/**************************************************************
* Constructors                                                *
***************************************************************/
ProjectionPlane::ProjectionPlane(cInt iSWidth, cInt iSHeight)
			: mFOV(700.0f), mClippingDistance(0.05f), mEyeDistance(40.0f) {
	SetViewport(iSWidth, iSHeight);
}
    
/**************************************************************
* Operators                                                   *
***************************************************************/


/**************************************************************
* Other helpfull functions                                    *
***************************************************************/
cBool ProjectionPlane::Clip(Point4D &a, Point4D &b) {
	cFloat fClipDistance = GetNearClippingPlane();

	if (a.mW < fClipDistance && b.mW < fClipDistance) {
		return false;
	}

	if (a.mW > b.mW) {
		Point4D temp = a;
		a = b;
		b = temp;
	}
	if (a.mW < fClipDistance) {
		a = b + (a - b) * ((fClipDistance - b.mW) / (a.mW - b.mW));
	}

	return true;
}

cVoid ProjectionPlane::SetViewport(cInt iSWidth, cInt iSHeight) {
	mWindowWidth = mVieportWidth = (cFloat) iSWidth;
	mWindowHeight = mVieportHeight = (cFloat) iSHeight;
}

cVoid ProjectionPlane::ChangeFov(cFloat fFov) {
	if (mFOV + fFov < 10.0f) {
		return;
	}
	mFOV += fFov;
}

cFloat ProjectionPlane::GetFOV() {
	return mFOV;
}

cVoid ProjectionPlane::ChangeEyeDistance(cFloat fEyeDistanceDelta) {
	if (mEyeDistance + fEyeDistanceDelta < 1.0f) {
		return;
	}
	mEyeDistance += fEyeDistanceDelta;
}

cFloat ProjectionPlane::GetEyeDistance() {
	return mEyeDistance;
}

cVoid ProjectionPlane::ChangeClipping(cFloat fClipping) {
	if ((fClipping + mClippingDistance) < 0.05 || (fClipping + mClippingDistance) > 10.0f) {
		return;
	}
	mClippingDistance += fClipping;
}

cFloat ProjectionPlane::GetNearClippingPlane() {
	return mClippingDistance;
}